//
//  P1088Mars-people.cpp
//  Algorithm
//
//  Created by 刘锦程 on 2023/9/9.
//

#include <stdio.h>
#include<iostream>
#include<cstdio>
using namespace std;
int book[10003],N,M;
int dfsn[10];
int ra[1000][10];
int ini[10003];
int   rankk[1000][10]={{0,0,0,0,0,0},{1,0,0,0,0,0},{1,2,0,0,0,0},{2,1,0,0,0,0},{1,2,3,0,0,0},{1,3,2,0,0,0},{2,1,3,0,0,0},{2,3,1,0,0,0},{3,1,2,0,0,0},{3,2,1,0,0,0},{1,2,3,4,0,0},{1,2,4,3,0,0},{1,3,2,4,0,0},{1,3,4,2,0,0},{1,4,2,3,0,0},{1,4,3,2,0,0},{2,1,3,4,0,0},{2,1,4,3,0,0},{2,3,1,4,0,0},{2,3,4,1,0,0},{2,4,1,3,0,0},{2,4,3,1,0,0},{3,1,2,4,0,0},{3,1,4,2,0,0},{3,2,1,4,0,0},{3,2,4,1,0,0},{3,4,1,2,0,0},{3,4,2,1,0,0},{4,1,2,3,0,0},{4,1,3,2,0,0},{4,2,1,3,0,0},{4,2,3,1,0,0},{4,3,1,2,0,0},{4,3,2,1,0,0},{1,2,3,4,5,0},{1,2,3,5,4,0},{1,2,4,3,5,0},{1,2,4,5,3,0},{1,2,5,3,4,0},{1,2,5,4,3,0},{1,3,2,4,5,0},{1,3,2,5,4,0},{1,3,4,2,5,0},{1,3,4,5,2,0},{1,3,5,2,4,0},{1,3,5,4,2,0},{1,4,2,3,5,0},{1,4,2,5,3,0},{1,4,3,2,5,0},{1,4,3,5,2,0},{1,4,5,2,3,0},{1,4,5,3,2,0},{1,5,2,3,4,0},{1,5,2,4,3,0},{1,5,3,2,4,0},{1,5,3,4,2,0},{1,5,4,2,3,0},{1,5,4,3,2,0},{2,1,3,4,5,0},{2,1,3,5,4,0},{2,1,4,3,5,0},{2,1,4,5,3,0},{2,1,5,3,4,0},{2,1,5,4,3,0},{2,3,1,4,5,0},{2,3,1,5,4,0},{2,3,4,1,5,0},{2,3,4,5,1,0},{2,3,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6,5,2,1,3,4},{6,5,2,1,4,3},{6,5,2,3,1,4},{6,5,2,3,4,1},{6,5,2,4,1,3},{6,5,2,4,3,1},{6,5,3,1,2,4},{6,5,3,1,4,2},{6,5,3,2,1,4},{6,5,3,2,4,1},{6,5,3,4,1,2},{6,5,3,4,2,1},{6,5,4,1,2,3},{6,5,4,1,3,2},{6,5,4,2,1,3},{6,5,4,2,3,1},{6,5,4,3,1,2},{6,5,4,3,2,1}};
bool same(int *a,int k,int *b){
    for(int i=0;i<k;i++){
        if(a[i]!=b[i]) return 0;
    }
        return 1;
}
int search6(int n,int* init){
    int hang=1;
    if(n==2){
        hang=2;
        while(!same(init,2,rankk[hang])){
            hang++;
        }
    }
    if(n==3){
        hang=4;
        while(!same(init,3,rankk[hang])){
            hang++;
        }
    }
    if(n==4){
        hang=10;
        while(!same(init,4,rankk[hang])){
            hang++;
        }
    }
    if(n==5){
        hang=34;
        while(!same(init,5,rankk[hang])){
            hang++;
        }
    }
    if(n==6){
        hang=154;
        while(!same(init,6,rankk[hang])){
            hang++;
        }
    }
    return hang;
}
int P1088ans(){
    scanf("%d%d",&N,&M);
    for(int i=0;i<N;i++){
        scanf("%d",&ini[i]);
    }
    if(N==1){
        return 0;
    }
    if(N<=6){
        int z=0;
        int hang= search6(N,ini)+M;
        while(z<6&&rankk[hang][z]!=0){
            printf("%d ",rankk[hang][z]);
            z++;
        }
    }else{
        int ini6[10]={};
        int shunxu[10]={};
        int zz=0;
        for(int i=N-6;i<=N-1;i++){
            ini6[i-N+7]=ini[i];
        }
        sort(ini+N-6,ini+N);
        for(int i=N-6;i<=N-1;i++){
            dfsn[i-N+7]=ini[i];
        }
        for(int i=1;i<=6;i++){
            for(int j=1;j<=6;j++){
                if(ini6[i]==dfsn[j]){
                    shunxu[zz++]=j;
                    continue;
                }
            }
        }
        int z=0;
        int hang= search6(6,shunxu)+M;
        if(hang>)
        for(int i=0;i<N-6;i++) cout<<ini[i]<<" ";
        while(z<6&&rankk[hang][z]!=0){
            printf("%d ",dfsn[rankk[hang][z]]);
            z++;
        }
    }
    printf("\n");
    return 0;
}
